package cn.edu.scau.cmi.liguanwu.hibernate.client;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.liguanwu.util.ScauCmiHibernateSessionFactoryUtil;
import cn.edu.scau.cmi.liguanwu.util.SpringDAOUtil;
import cn.edu.scau.cmi.liguanwu.DAO.StudentDAO;
import cn.edu.scau.cmi.liguanwu.DAO.TeacherDAO;
import cn.edu.scau.cmi.liguanwu.domain.Student;
import cn.edu.scau.cmi.liguanwu.domain.Teacher;
import cn.edu.scau.cmi.liguanwu.hibernate.factory.ScauCmiHibernateSessionFactory;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

public class Controller implements Initializable{
	@FXML
	private Button button1;
	@FXML
	private Button button2;
	@FXML
	private Button button4;
	@FXML
	private TextField textfield_stu_id;
	@FXML
	private TextField textfield_stu_name;
	@FXML
	private TextField textfield_stu_tutor;
	@FXML
	private TextField textfield_tea_id;
	@FXML
	private TextField textfield_tea_name;
	@FXML
	private TextArea textarea;
	ApplicationContext applicationContext = SpringDAOUtil.getApplicationContext();
	StudentDAO studentDAO=(StudentDAO) applicationContext.getBean("studentDAO");
	TeacherDAO teacherDAO=(TeacherDAO) applicationContext.getBean("teacherDAO");
	Teacher teacher = new Teacher();
	Student student = new Student();
	String s_id,s_name,s_tutor,t_id,t_name;
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		
	}
    public void onClick1(ActionEvent event) {
    	s_id=textfield_stu_id.getText();
		s_name=textfield_stu_name.getText();
		s_tutor=textfield_stu_tutor.getText();
		t_id=textfield_tea_id.getText();
		t_name=textfield_tea_name.getText();
		if(s_id.length()!=0&&s_name.length()!=0) {
			textarea.setText("");
			student.setId(Long.parseLong(s_id));
			student.setName(s_name);
			if(s_tutor.length()!=0)
				student.setTeacher(teacherDAO.findById(Long.parseLong(s_tutor)));
			studentDAO.save(student);
			listStudents();
		}
		else if(t_id.length()!=0&&t_name.length()!=0) {
			textarea.setText("");
			teacher.setId(Long.parseLong(t_id));
			teacher.setName(t_name);
			teacherDAO.save(teacher);
			listTeachers();
		}
		else{
			textarea.setText("ERROR! ID and name can't be empty. ");
		}
	}
    public void onClick2(ActionEvent event) {
    	s_id=textfield_stu_id.getText();
    	t_id=textfield_tea_id.getText();
    	if(s_id.length()!=0) {
    		textarea.setText("");
    		studentDAO.delete(studentDAO.findById(Long.parseLong(s_id)));
    		listStudents();
    	}
    	else if(t_id.length()!=0) {
    		textarea.setText("");
    		teacherDAO.delete(teacherDAO.findById(Long.parseLong(t_id)));
    		listTeachers();
    	}
    	else {
    		textarea.setText("Can only be deleted by entering ID");
    	}
	}
    public void onClick3(ActionEvent event) {
    	s_id=textfield_stu_id.getText();
		s_name=textfield_stu_name.getText();
		s_tutor=textfield_stu_tutor.getText();
		t_id=textfield_tea_id.getText();
		t_name=textfield_tea_name.getText();
		if(s_id.length()!=0) {
			textarea.setText("");
			student.setId(Long.parseLong(s_id));
			if(s_name.length()!=0)
				student.setName(s_name);
			else 
				student.setName(studentDAO.findById(Long.parseLong(s_id)).getName());
			if(s_tutor.length()!=0) 
				student.setTeacher(teacherDAO.findById(Long.parseLong(s_tutor)));
			else
				student.setTeacher(studentDAO.findById(Long.parseLong(s_id)).getTeacher());
			Session session = ScauCmiHibernateSessionFactory.getSession();
			Transaction transaction = session.beginTransaction();
			session.merge(student);
			transaction.commit();
			session.close();
			listStudents();
		}
		else if(t_id.length()!=0) {
			textarea.setText("");
			teacher.setId(Long.parseLong(t_id));
			if(t_name.length()!=0)
				teacher.setName(t_name);
			else 
				teacher.setName(teacherDAO.findById(Long.parseLong(t_id)).getName());
			Session session = ScauCmiHibernateSessionFactory.getSession();
			Transaction transaction = session.beginTransaction();
			session.merge(teacher);
			transaction.commit();
			session.close();
			listTeachers();
		}
		else {
			textarea.setText("Please update through ID");
		}
	}
    private static ApplicationContext getApplicationContext1() {
		ApplicationContext ac1 = new ClassPathXmlApplicationContext("applicationContext.xml");

		return ac1;
	}
    private  void listStudents() {
		String buffer="";
		StudentDAO StudentDAO = new StudentDAO();
		List Students = StudentDAO.findAll();
		Iterator<?> StudentIterator = Students.iterator();
		while (StudentIterator.hasNext()) {
			Student student = (Student) StudentIterator.next();
			if(student.getTeacher()==null)
				buffer+=student.getId()+"\t\t"+student.getName()+"\t\t\t"+null+"\n";
			else
			buffer+=student.getId()+"\t\t"+student.getName()+"\t\t\t"+student.getTeacher().getId()+"\n";
		}
		textarea. setText("ID\t\t\tName\t\tTutor\n"+buffer);
	}
	private  void listTeachers() {
		String buffer="";
		TeacherDAO TeacherDAO = new TeacherDAO();
		List Teachers = TeacherDAO.findAll();
		Iterator<?> TeacherIterator = Teachers.iterator();
		while (TeacherIterator.hasNext()) {
			Teacher teacher = (Teacher) TeacherIterator.next();
				buffer+=teacher.getId()+"\t\t"+teacher.getName()+"\n";
		}
		textarea. setText("ID\t\tName\n"+buffer);
	}
}